package com.cqs.leetcode.link;

import com.cqs.leetcode.ListNode;

/**
 * lixw
 * 2021/11/2
 **/
public class OddEvenLinkedList328 {

    //
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode oHead = head,eHead = head.next;
        oHead.next = null;
        ListNode cur = eHead.next, next = null;
        eHead.next = null;
        ListNode oTail = oHead, eTail = eHead;
        int num = 2;
        while (cur != null) {
            next = cur.next;
            cur.next = null;
            if (++num % 2 == 1 ) {
                oTail.next = cur;
                oTail = cur;
            }else {
                eTail.next = cur;
                eTail = cur;
            }
            cur = next;
        }
        oTail.next = eHead;
        return oHead;
    }
}
